﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_DSJCJ.Common;
using System.Data;
using Webdiyer.WebControls.Mvc;

namespace ElingOA6_DSJCJ.Controllers
{
    /// <summary>
    /// 软件下载
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class SoftwareController : Controller
    {
        //
        // GET: /Software/

        private BLL.SoftwareCatalog softwareCatalogBLL = new BLL.SoftwareCatalog();
        private BLL.SoftwareEntry softwareEntryBLL = new BLL.SoftwareEntry();
        private BLL.Common commonBLL = new BLL.Common();
        /// <summary>
        /// 软件下载首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            string entityNo = GlobalConstant.ENTITYNO;
            List<Model.SoftwareCatalog> softCatalogList = softwareCatalogBLL.GetModelListByFEntityNo(entityNo);
            List<Model.SoftwareEntry> softEntryList = softwareEntryBLL.GetModelListByFEntityNo(entityNo);
            foreach (Model.SoftwareCatalog softCatalogModel in softCatalogList)
            {
                int count = softEntryList.Where(s => s.FSoftwareCatalogID == softCatalogModel.FSoftwareCatalogID).ToList().Count;
                softCatalogModel.SoftwareCount = count;
            }

            return View(softCatalogList);
        }
        /// <summary>
        /// 软件列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult SoftwareList(int id = 1)
        {
            string entityNo = GlobalConstant.ENTITYNO;
            int pageSize = 15;
            int pageIndex = id;
            string fieldOrder = " FSoftwareEntryID";
            string strWhere = "";
            string fiedls = "*";
            string tableName = "t_SoftwareEntry";
            decimal softwareid = decimal.Parse(Fetch.Get("softwareid"));
            Model.SoftwareCatalog softCatalogModel = softwareCatalogBLL.GetModel(softwareid, entityNo);
            if (softCatalogModel != null)
            {
                ViewData["softwareCatalogName"] = softCatalogModel.FSoftwareCatalogName;
                strWhere = " FEntityNo='" + entityNo + "' and FSoftwareCatalogID=" + softwareid + "";
                int total = 0;

                DataSet ds = commonBLL.GetListByPage(tableName, fiedls, pageSize, pageIndex, strWhere, fieldOrder, 1, out total);
                List<Model.SoftwareEntry> tmpSoftwareEntryList = softwareEntryBLL.DataTableToList(ds.Tables[0]);

                PagedList<Model.SoftwareEntry> softwareEntryList = new PagedList<Model.SoftwareEntry>(tmpSoftwareEntryList, id, pageSize, total);
                return View(softwareEntryList);
            }
            else
            {
                return View("Error");
            }
        }
        /// <summary>
        /// 软件下载
        /// </summary>
        /// <returns></returns>
        public ActionResult SoftwareDownLoad(decimal softwareEntryID)
        {
            try
            {
                Model.SoftwareEntry softwareEntryModel = softwareEntryBLL.GetModel(softwareEntryID);
                if (softwareEntryModel != null)
                {
                    softwareEntryBLL.UpdateDownCount(softwareEntryID);
                    return new Common.FileResult()
                    {
                        FileName = softwareEntryModel.FAttachOldFileName,
                        FilePaht = Server.MapPath(softwareEntryModel.FSoftwareSvrFileName)

                    };
                }
                else
                {
                    return View("Error");
                }
            }
            catch
            {
                return View("Error");
            }
            
        }
    }
}
